/**
 * 描述：文件内容替换
 * 包名：org.file.pom.version
 * 文件名：PomVersionManager.java
 * 作者：eddy
 * 日期：2017年4月14日-下午6:27:55
 * 
 */
package org.file.pom.version;

import java.io.File;
import java.io.IOException;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.Element;

/**
 * 文件内容替换
 * 
 * <pre>
 *  
 * 构建组：e-pom
 * 作者：eddy
 * 邮箱：xqxyxchy@126.com
 * 日期：2017年4月14日-下午6:27:55
 * </pre>
 */
public class PomVersionManager {

	/**
	 * 执行
	 *
	 * @param args
	 * @throws IOException
	 */
	public static void main(String[] args) throws IOException {
		// SNAPSHOT or LC.RELEASE

		String path = ".";
		String scVersion = "SNAPSHOT";
		String targetVersion = "LC.RELEASE";

		String autoVersion = "false";
		String step = "1";

		if (BeanUtil.isNotEmpty(args)) {
			path = args[0];
		}
		if (BeanUtil.isNotEmpty(args) && args.length > 1) {
			scVersion = args[1];
		}
		if (BeanUtil.isNotEmpty(args) && args.length > 2) {
			targetVersion = args[2];
		}
		if (BeanUtil.isNotEmpty(args) && args.length > 3) {
			autoVersion = args[3];
		}
		if (BeanUtil.isNotEmpty(args) && args.length > 4) {
			step = args[4];
		}

		boolean isAutoVersion = Boolean.valueOf(autoVersion);
		List<File> poms = FileUtil.list(path, "pom.xml", 4);

		replace(poms, scVersion, targetVersion, isAutoVersion, step);
	}

	public static void replace(List<File> poms, String scVersion, String targetVersion, boolean isAutoVersion,
			String step) throws IOException {
		if (BeanUtil.isEmpty(poms)) {
			return;
		}

		Document doc = null;
		for (File pom : poms) {
			doc = Dom4jUtil.load(pom.getAbsolutePath());
			boolean isReplace = replace(doc, scVersion, targetVersion, isAutoVersion, step);
			if (isReplace) {
				FileUtil.console(pom);
				Dom4jUtil.write(doc, pom.getAbsolutePath());
			}
		}
	}

	public static boolean replace(Document doc, String scVersion, String targetVersion, boolean isAutoVersion,
			String step) {
		boolean isReplace = false;
		List<Element> versionElements = Dom4jUtil.selectEndWithText(doc.getRootElement(), scVersion);
		String join = "-";
		String dot = ".";
		String[] stepArr = step.split(",");
		if (stepArr.length != 2) {
			stepArr = new String[] { "0", "1" };
		} else {
			if (!BeanUtil.isNumber(stepArr[0])) {
				stepArr[0] = "0";
			}
			if (!BeanUtil.isNumber(stepArr[1])) {
				stepArr[1] = "1";
			}
		}

		int idx = Integer.valueOf(stepArr[0]).intValue();
		int vstep = Integer.valueOf(stepArr[1]).intValue();

		for (Element version : versionElements) {
			String oText = version.getTextTrim();
			String[] oTextArr = oText.split(join);
			if (oTextArr.length == 2) {
				isReplace = true;
				oTextArr[1] = targetVersion;

				if (isAutoVersion) {
					String[] oTextVArr = oTextArr[0].split(dot);
					if ((idx > -1) && (idx < oTextVArr.length) && (BeanUtil.isNumber(oTextVArr[idx]))) {
						oTextVArr[idx] = String.valueOf(Integer.valueOf(oTextVArr[idx]).intValue() + vstep);
						oTextArr[0] = StringUtil.join(oTextVArr, dot);
					}
				}

				version.setText(StringUtil.join(oTextArr, join));
			}
		}
		return isReplace;
	}

}
